package org.lem.auth

import org.apache.shiro.crypto.hash.Sha1Hash
import org.apache.shiro.crypto.hash.Sha512Hash

class ShiroUserController
{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [shiroUserInstanceList: ShiroUser.list(params), shiroUserInstanceTotal: ShiroUser.count()]
    }

    def create = { 
    }

    def save = { LoginCreateCommand cmd ->
        //def shiroUserInstance = new ShiroUser(params)
        if (cmd.validate())
        {
            def shiroUserInstance = cmd.createLogin()
            if (shiroUserInstance.save(flush: true))
            {
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'shiroUser.label', default: 'Login'), shiroUserInstance.username])}"
                redirect(action: "show", id: shiroUserInstance.id)
            }
        }
        else
        {
            render(view: "create", model: [loginCreateCommand: cmd])
        }
    }

    def show = {
        def shiroUserInstance = ShiroUser.get(params.id)
        if (!shiroUserInstance)
        {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'shiroUser.label', default: 'Login'), params.id])}"
            redirect(action: "list")
        }
        else
        {
            [shiroUserInstance: shiroUserInstance]
        }
    }

    def edit = {
        def shiroUserInstance = ShiroUser.get(params.id)
        if (!shiroUserInstance)
        {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'shiroUser.label', default: 'Login'), params.id])}"
            redirect(action: "list")
        }
        else
        {
            return [shiroUserInstance: shiroUserInstance]
        }
    }

    def update = { LoginEditCommand cmd ->
        def shiroUserInstance = ShiroUser.get(params.id)
        // if there are no errors in the command object, set the properties of the shiroUserInstance
        if (shiroUserInstance)
        {
            if (cmd.hasErrors()) {
                render(view: "edit", model: [shiroUserInstance: shiroUserInstance, loginEditCommand:cmd])
            }
            else
            {
                if (params.version)
                {
                    def version = params.version.toLong()
                    if (shiroUserInstance.version > version)
                    {

                        shiroUserInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'shiroUser.label', default: 'Login')] as Object[], "Another user has updated this Login while you were editing")
                        render(view: "edit", model: [shiroUserInstance: shiroUserInstance])
                        return
                    }
                }

                shiroUserInstance.username = cmd.username
                shiroUserInstance.roles = []
                cmd.roles.each {
                    shiroUserInstance.roles.add(ShiroRole.get(it))
                }
                if (cmd.password) {
                    shiroUserInstance.passwordHash = new Sha512Hash(cmd.password).toHex()
                }

                // if there were no errors, we can attempt to save the shiroUserInstance with the new values
                if (shiroUserInstance.save(flush: true))
                {
                    flash.message = "${message(code: 'default.updated.message', args: [message(code: 'shiroUser.label', default: 'Login'), shiroUserInstance.username])}"
                    redirect(action: "show", id: shiroUserInstance.id)
                }
                else
                {
                    render(view: "edit", model: [shiroUserInstance: shiroUserInstance])
                }
            }
        }
        else
        {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'shiroUser.label', default: 'Login'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def shiroUserInstance = ShiroUser.get(params.id)
        if (shiroUserInstance)
        {
            try
            {
                shiroUserInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'shiroUser.label', default: 'Login'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e)
            {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'shiroUser.label', default: 'Login'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else
        {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'shiroUser.label', default: 'Login'), params.id])}"
            redirect(action: "list")
        }
    }

}
